package com.tobeface.sns.interfaces.admin;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tobeface.sns.application.security.UserService;
import com.tobeface.sns.domain.User;
import com.tobeface.sns.interfaces.constants.SubjectAttrConstants;
import com.tobeface.sns.interfaces.support.SubjectAwareController;

/**
 * 
 * @author loudyn
 * 
 */
@Controller
public class AdminController extends SubjectAwareController {

	private UserService userService;

	@Autowired
	public AdminController(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/user/forget", method = GET)
	public String forget() {
		return "admin/user/forget";
	}

	/**
	 * 
	 * @param username
	 * @param email
	 * @return
	 */
	@RequestMapping(value = "/user/forget", method = POST)
	public String forget(@RequestParam("username") String username, @RequestParam("email") String email) {
		try {

			userService.forgetPassword(username, email);
		} catch (Exception e) {
			return "redirect:/user/forget/error";
		}

		return "redirect:/user/forget/ok";
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/user/forget/error", method = GET)
	public String forgetError() {
		return "admin/user/forget-error";
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/user/forget/ok", method = GET)
	public String forgetOk() {
		return "admin/user/forget-ok";
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/user/password/edit", method = GET)
	public String password() {
		return "admin/user/password";
	}

	@RequestMapping(value = "/user/password/edit", method = POST)
	public String updatePassword(String oldPassword, String newPassword) {
		User user = (User) getSubject().getSession().getAttribute(SubjectAttrConstants.USER_AWARE_CONSTANT);
		if (user.getMember().isCorrectPassword(oldPassword)) {
			userService.updatePassword(user.getMember().getUsername(), newPassword);
			User updateUser = userService.getByUsername(user.getMember().getUsername());
			getSubject().getSession().setAttribute(SubjectAttrConstants.USER_AWARE_CONSTANT, updateUser);
		}
		return "redirect:/user/password/edit";
	}

	@RequestMapping(value = "/oldPassword/isCorrect", method = RequestMethod.GET)
	@ResponseBody
	public boolean isCorrectPassword(HttpServletRequest request, @RequestParam("oldPassword") String oldPassword) {
		User user = (User) getSubject().getSession().getAttribute(SubjectAttrConstants.USER_AWARE_CONSTANT);
		return user.getMember().isCorrectPassword(oldPassword);
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/admin/dashboard", method = GET)
	public String dashboard(Model model) {
		return "/home";
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/admin/logout", method = GET)
	public String logout() {
		getSubject().logout();
		return "redirect:/login";
	}

	@RequestMapping(value = "/admin/centre", method = GET)
	public String centre(Model model) {
		return "admin/centre";
	}

	@RequestMapping(value = "/supply/centre", method = GET)
	public String supply() {
		return "supply-information/youboy";
	}

	@RequestMapping(value = "/user/serve/list", method = GET)
	public String serve() {
		return "admin/serve/list";
	}

	@RequestMapping(value = "/user/serve/center", method = GET)
	public String center() {
		return "admin/serve/youboy";

	}

	@RequestMapping(value = "user/account/edit", method = GET)
	public String account() {
		return "admin/account/form";
	}

	@RequestMapping(value = "user/email/edit", method = GET)
	public String email() {
		return "admin/email/form";
	}

	@RequestMapping(value = "/oldEmail/isCorrect", method = RequestMethod.GET)
	@ResponseBody
	public boolean isCorrectByUsernameAndEmail(HttpServletRequest request, @RequestParam("oldEmail") String oldEmail) {
		User user = (User) getSubject().getSession().getAttribute(SubjectAttrConstants.USER_AWARE_CONSTANT);
		boolean sd = userService.existsByUsernameAndEmail(user.getMember().getUsername(), oldEmail);
		return sd;
	}

	@RequestMapping(value = "user/email/edit", method = POST)
	public String updateEmail(String oldEmail, String newEmail) {
		User user = (User) getSubject().getSession().getAttribute(SubjectAttrConstants.USER_AWARE_CONSTANT);
		if (userService.existsByUsernameAndEmail(user.getMember().getUsername(), oldEmail)) {
			userService.updateEmail(user.getMember().getUsername(), newEmail);
			User updateUser = userService.getByUsername(user.getMember().getUsername());
			getSubject().getSession().setAttribute(SubjectAttrConstants.USER_AWARE_CONSTANT, updateUser);
		}
		return "admin/email/form";
	}

	@RequestMapping(value = "user/vip/list", method = GET)
	public String vip() {
		return "admin/vip/list";
	}

}
